package com.sure0000.basic;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

/**
 * @program: demo
 * @description: LocalDateTime is thread safe
 * @author: xuyouchang@supcon.com
 * @create: 2019-12-25 09:25
 **/
public class TimeClass {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime rightNow = LocalDateTime.now();
        System.out.println("当前时刻：" + rightNow);
        System.out.println("当前年份：" + rightNow.getYear());
        System.out.println("当前月份：" + rightNow.getMonth());
        System.out.println("当前日分：" + rightNow.getDayOfMonth());
        System.out.println("当前时：" + rightNow.getHour());
        System.out.println("当前分：" + rightNow.getMinute());
        System.out.println("当前秒：" + rightNow.getSecond());

        // 构造时间
        LocalDateTime beforeDate = LocalDateTime.of(2019, Month.DECEMBER, 12, 9, 21, 32);
        System.out.println(beforeDate);

        // 修改日期
        LocalDateTime now = LocalDateTime.now();
        now = now.minusYears(1);
        now = now.minusMonths(1);
        System.out.println(now);

        // 格式化日期
        LocalDateTime nowTime = LocalDateTime.now();
        String res1 = nowTime.format(DateTimeFormatter.BASIC_ISO_DATE);
        String res2 = nowTime.format(DateTimeFormatter.ISO_DATE);
        String res3 = nowTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
        System.out.println(res1);
        System.out.println(res2);
        System.out.println(res3);

        // 字符串解析
        LocalDateTime time = LocalDateTime.parse("2002--01--02 11:23", DateTimeFormatter.ofPattern("yyyy--MM--dd HH:mm"));
        System.out.println("字符串解析：" + time);

    }
}
